# from appium import webdriver
# import time
# import datetime
# import pymysql
#
# import asyncio
# from threading import Thread
# import uuid
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.common.by import By
# from qxb_pojo import qxb_pojo
#
# '''
# platformName（系统名称）：Android、IOS、Windows、Linux
# plaformVersion（系统版本）
# deviceName（设备名称，即设备运行的地址，默认为127.0.0.1:62001）
# appPackage（要打开哪个软件）、appActivity（要打开的软件的活动页面）
# appPackage、appActivity两个参数可以通过点开模拟器上想要打开的软件，然后通过命令行获取
# noReset  建议noReset设置为true, 不然每次程序打开的app都是初始状态，考虑到有些app需要登录以后才能拿到数据
#
# '''
#
#
# class qxb_spider(object):
#     """ 初始化apinum参数及数据库连接"""
#
#     def __init__(self):
#         desired_caps = {
#             "platformName": "Android",
#             "platformVersion": "9",
#             "deviceName": "53a576ff",
#             "appPackage": "com.bertadata.qxb",
#             "automationName": "UiAutomator1",
#             "appActivity": "com.bertadata.qxb.activity.SplashActivity",
#             'noReset': True,
#             'unicodeKeyboard': True,
#         }
#
#         # 1. 进入app，并定位到主页
#         self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
#         time.sleep(10)  # 等待开屏广告
#         self.driver.implicitly_wait(1)  # 隐式等待页面刷新 max 10s
#         # 2. 读取分辨率备用
#         self.height = int(self.driver.get_window_size()['height'])
#         self.width = int(self.driver.get_window_size()['width']) * 0.5
#
#         # 创建连接
#         # self.conn = pymysql.connect(host='192.168.75.147', port=3306, user='root', passwd='123456', db='zouyao_test')
#         # # 创建游标
#         # self.cursor = self.conn.cursor()
#         # self.loop = asyncio.get_event_loop()
#
#     """ 回退方法 """
#
#     def back_to_search(self):
#         back_btn_xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget" \
#                          ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup" \
#                          "/android.widget.ImageView[1] "
#         self.driver.find_element_by_xpath(back_btn_xpath).click()
#
#     """ 首次进入 """
#
#     def first_in(self):
#         driver = self.driver
#         driver.find_element_by_xpath("//*[@text='请输入企业名、股东高管、地址等']").click()
#
#     """ 处理弹窗广告 """
#
#     def always_allow(self, num=2):
#         allow_xpath = ("xpath", "//*[text='总是允许']")
#
#         pass
#
#     """ 企业信息汇总 """
#
#     def get_info(self, ent_name):
#
#         ent_info_list = [ent_name]
#         # driver.find_element_by_xpath("//*[@text='请输入企业名称、股东高管、品牌等']").send_keys(ent_name)
#         ent_input_xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget" \
#                           ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget" \
#                           ".LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget" \
#                           ".LinearLayout/android.widget.EditText "
#
#         driver = self.driver
#         driver.find_element_by_xpath(ent_input_xpath).clear()
#         driver.find_element_by_xpath(ent_input_xpath).send_keys(ent_name)
#
#         # time.sleep(5)
#         # 5. 默认选取第一家企业,点击进入
#         ent_first_div_xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget" \
#                               ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget" \
#                               ".LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget" \
#                               ".LinearLayout/android.view.ViewGroup/android.view.ViewGroup/android.support.v7.widget" \
#                               ".RecyclerView/android.view.ViewGroup[1] "
#         while True:
#             try:
#                 driver.find_element_by_xpath(ent_first_div_xpath).click()
#                 break
#             except Exception as e:
#                 time.sleep(1)
#
#         ent_info_overview_title = ["企业年报", "行政处罚", "环保处罚", "经营异常", "税务异常", "严重违法失信", "司法拍卖", "清算信息", "破产重整", "新闻舆情"]
#         for item in ent_info_overview_title:
#             self.swipe_and_search_overview(item, ent_info_list)
#
#         # self.loop.run_until_complete(self.save_info(ent_info_list))
#         # 回到原输入框
#         self.back_to_search()
#
#     """ 企业年报列表页 """
#
#     def get_ent_report_info(self, ent_name):
#         report_title_xpath = "//*[contains(@text, '年度')]"
#         report_title_list = self.driver.find_elements_by_xpath(report_title_xpath)
#         # 存储每个列表
#         for item in report_title_list:
#             report_title = item.get_attribute("text")
#             print(report_title)
#             report__title_time = item.find_element_by_xpath(".//android.widget.TextView[2]")
#             print(report__title_time)
#             # 进入该年报的详情页
#
#         # 无图片时表示该页信息未结束，下滑一页
#
#     """ 企业年报详情页 """
#
#     def get_ent_report_detail(self, report_name):
#         pass
#
#     """ 滑动并搜索企业概览页面相关信息 """
#
#     def swipe_and_search_overview(self, title, ent_info):
#         while True:
#             try:
#                 self.driver.find_element_by_xpath("//android.widget.TextView[@text='{}']".format(title))  # 寻找元素
#                 base_element = "//android.widget.TextView[@text='{}']/preceding-sibling::android.widget.TextView[1]"
#                 info = self.driver.find_element_by_xpath(base_element.format(title))
#                 ent_info.append(info.get_attribute("text"))
#                 print(title)
#                 print(ent_info)
#                 if title == '企业年报':
#                     info.click()
#                     print(title)
#                     self.get_ent_report_info('xxx')
#                 break
#             except Exception as e:
#                 self.driver.swipe(self.width, self.height * 0.8, self.width, self.height * 0.4)  # 滑动屏幕
#
#     # //*[contains(@text, "年度")]
#
#     """ 持久化方法概览页面方法 """
#
#     async def save_info(self, data):
#         qxb_sql = '''
#         INSERT INTO zouyao_test.qxb
#             (ent_name, ent_year_report, administration_punish,
#             environment_punish, manage_exception, tax_exception,
#             outrage, judicial_sale, clearing_info, re_habilitation, news)
#         VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
#         '''
#         self.cursor.execute(qxb_sql, data)
#         self.conn.commit()
#
#
# # test
# qxb = qxb_spider()
# qxb.first_in()
# qxb.get_info('北京阿里巴巴信息技术有限公司')
# start = datetime.datetime.now()
# qxb.get_info('拼多多')
# qxb.get_info('老干妈')
# qxb.get_info('vivo')
# qxb.get_info('小米')
# qxb.get_info('擎天科技')
#
# end = datetime.datetime.now()
# print(end - start)
